package com.karl.loading.loadings;

import android.animation.ValueAnimator;
import android.content.Context;
import android.graphics.Camera;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.RectF;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;

import com.karl.loading.base.BaseProgressView;

/**
 * Created by Karl on 2017/8/28 0028.
 * desc:
 */

public class ITHomeLoadingView extends BaseProgressView {

    //画笔
    private Paint mPaint;

    //画圆弧用
    private RectF mRectF;

    //测量字体边界
    private Rect mRect;

    //3D专用
    private Camera mCamera;

    private Matrix mMatrix;

    //字体颜色
    private int mTextColor = Color.WHITE;

    //字体大小
    private float mTextSize = 15f;

    //关键字
    private String mKeyword = "IT";

    //主题色
    private int mHomeColor = Color.RED;

    //下拉进度
    private float progress;

    //翻转系数
    private float degree;

    //圆环与圆间距
    private float mRingSpace = DEFAULT_STROKE_WIDTH;

    //圆环宽度
    private float mStrokeWidth = DEFAULT_STROKE_WIDTH;

    public ITHomeLoadingView(Context context) {
        this(context,null);
    }

    public ITHomeLoadingView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        setMaxProgress(360);
        //关闭硬件加速
        if (isHardwareAccelerated()) this.setLayerType(View.LAYER_TYPE_SOFTWARE, null);

        mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mPaint.setStyle(Paint.Style.STROKE);
        mPaint.setStrokeWidth(dp2px(mStrokeWidth));
        mPaint.setFakeBoldText(true);
        mPaint.setColor(mHomeColor);

        mCamera = new Camera();
        mMatrix = new Matrix();
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        float spacing = 10;
        float centerX = getWidth()/2;
        float centerY = getHeight()/2;

        //实心圆
        mPaint.setStrokeWidth(dp2px(mStrokeWidth));

        mPaint.setStyle(Paint.Style.FILL);
        //内圆半径，减去外圆环宽度，减去预留内间距，减去圆环与圆间距
        float radius = centerX - dp2px(mStrokeWidth)-spacing-dp2px(mRingSpace);
        canvas.drawCircle(centerX,centerX,radius,mPaint);

        //文字
        mPaint.setTextSize(sp2px(mTextSize));
        mPaint.setColor(mTextColor);
        if (mRect == null)mRect = new Rect();
        mPaint.getTextBounds(mKeyword,0,mKeyword.length(),mRect);
        float textWidth = mPaint.measureText(mKeyword);
        //画文字
        canvas.drawText(mKeyword,(getWidth()-textWidth)/2,(getHeight()-mRect.height())/2+mRect.height(),mPaint);

        switch (getCurrentMode()){
            case MODE_PROGRESS:
                mPaint.setColor(mHomeColor);
                mPaint.setStyle(Paint.Style.STROKE);
                if (mRectF == null){
                    mRectF = new RectF(spacing,spacing,getWidth()-spacing,getHeight()-spacing);
                }
                canvas.drawArc(mRectF,-90, progress,false,mPaint);
                break;
            case MODE_LOADING:

                mMatrix.reset();
                mCamera.save();
                mCamera.rotateY(degree);
                mCamera.getMatrix(mMatrix);
                mCamera.restore();

                mMatrix.preTranslate(-centerX,-centerY);
                mMatrix.postTranslate(centerX,centerY);
                canvas.concat(mMatrix);
                mPaint.setColor(mHomeColor);
                mPaint.setStyle(Paint.Style.STROKE);
                canvas.drawCircle(centerX,centerX,centerX-spacing,mPaint);

                break;
        }



    }

    @Override
    public void updateProgress(float progress) {
        super.updateProgress(progress);
        if (progress > getMaxProgress()){
            progress = getMaxProgress();
        }
        this.progress = progress;
        postInvalidate();
    }

    @Override
    public void setColor(int color) {
        this.mHomeColor = color;
        postInvalidate();
    }


    @Override
    protected ValueAnimator initAnim() {
        return ValueAnimator.ofFloat(0,180);
    }

    @Override
    public void startAnim() {
        startAnim(500);
    }

    @Override
    public void startAnim(long time) {
        startAnim(ValueAnimator.INFINITE,ValueAnimator.RESTART,time);
    }

    @Override
    protected void onUpdateAnim(ValueAnimator animator) {
        degree = (float) animator.getAnimatedValue();
        postInvalidate();
    }

}
